package com.bigtotoro.shuiyincamera.util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Build;
import android.os.Process;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;

import com.bigtotoro.shuiyincamera.MyApplication;


/**
 * Created by huzeyin on 2018/1/24.
 */

public class SystemUtils {

    /**
     * 获取当前SDK版本号
     *
     * @return
     */
    public static int getCurrentAPILevel() {
        return Build.VERSION.SDK_INT;
    }

    public static void restartMainProcess() {
        Process.killProcess(Process.myPid());
    }

    /**
     * 获取电话号(有可能为空)
     *
     * @param context
     * @return
     */
    public static String getPhone(Context context) {
        TelephonyManager mTelephonyMgr = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        String num = mTelephonyMgr.getLine1Number();

        if (TextUtils.isEmpty(num)) {
            return "";
        } else {
            return num;
        }
    }

    public static boolean isSystemRotationEnabled() {
        try {
//            return Settings.System.getInt(MyApplication.getAppContext().getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1;
            return Settings.System.getInt(MyApplication.getInstance().getApplicationContext().getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1;
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
    }

    /**
     * 获得屏幕分辨率x(宽)
     *
     * @return
     */
    public static int getScreenX() {
        return MyApplication.getInstance().getApplicationContext().getResources().getDisplayMetrics().widthPixels;
    }

    /**
     * 获得屏幕分辨率y(高)
     *
     * @return
     */
    public static int getScreenY() {
        return MyApplication.getInstance().getApplicationContext().getResources().getDisplayMetrics().heightPixels;
    }



    /**
     * 获取操作系统版本
     *
     * @return
     */
    public static String getOsVersion() {
        return Build.VERSION.RELEASE;
    }

    public static int getAndroidSDKVersion() {
        int version = 0;
        try {
            version = Integer.valueOf(Build.VERSION.SDK_INT);
        } catch (NumberFormatException e) {

        }
        return version;
    }

    public static String getVersion() {
        PackageManager packageManager = MyApplication.getInstance().getApplicationContext().getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(MyApplication.getInstance().getApplicationContext().getPackageName(), 0);
            return packageInfo.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * 判断activity是否处于栈顶
     *
     * @return true在栈顶false不在栈顶
     */
    public static boolean isMainActivityTop(Context context, Class<?> XXXActivity) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        String name = manager.getRunningTasks(1).get(0).topActivity.getClassName();
        if (name.equals(XXXActivity.getName())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 是否是高分辨率的手机
     *
     * @return
     */
    public static boolean isHighResolution() {
        DisplayMetrics dm = MyApplication.getInstance().getApplicationContext().getResources().getDisplayMetrics();
        return dm.widthPixels >= 1080 ? true : false;
    }

    /**
     * check current activity is finished
     *
     * @param activity the current activity
     * @return whether current activity is finished or not
     */
    public static boolean isActivityFinished(Activity activity) {
        if (null == activity) return true;
        if (activity.isFinishing()) return true;
        try {
            if (Build.VERSION.SDK_INT >= 17 && activity.isDestroyed()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    /**
     * convert dip to px
     */
    public static int dip2px(float dpValue) {
        final float scale = MyApplication.getInstance().getApplicationContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * convert px to dp
     */
    public static int px2dip(float pxValue) {
        final float scale = MyApplication.getInstance().getApplicationContext().getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int getStatusBarHeight() {
        Resources resources = MyApplication.getInstance().getApplicationContext().getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }

    public static int getNavigationBarHeight() {
        Resources resources = MyApplication.getInstance().getApplicationContext().getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    public static void setStatusBarAndBottomBarTranslucent(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = activity.getWindow();
            // Translucent status bar
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // Translucent navigation bar
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }
}
